package com.huanghao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.huanghao.Service.UserService;
import com.huanghao.pojo.Result;
import com.huanghao.pojo.User;
import com.huanghao.utils.JwtUtils;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;
    
    @PostMapping("login.do")
    public Result login( User user){
		System.out.println("登录");
        User e = userService.login(user); 
        //登录成功，生成令牌并且下发令牌
        if (e != null){
            Map<String, Object> claim = new HashMap<>();//封装当前登录的员工信息
            claim.put("uId",e.getuId());
            claim.put("uName",e.getuName());
            claim.put("uPassword",e.getuPassword());
            String jwt = JwtUtils.generateJwt(claim);//令牌中包含了当前登录的员工信息
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.error("用户名或密码错误");
    }
    
    
}
